package com.honghu.villagepost.adapter;

import android.view.View;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.honghu.villagepost.R;
import com.honghu.villagepost.activity.StoreDetailsActivity;
import com.honghu.villagepost.entity.dao.GoodsEntity;
import com.honghu.villagepost.entity.dao.StoreEntity;
import com.honghu.villagepost.util.DensityUtil;
import com.honghu.villagepost.util.GlideUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Desc:购物车列表适配器
 * <p>
 * Author: [李豫]
 * Date: 2021-07-14
 * Copyright: Copyright (c) 2010-2021
 * Updater:
 * Update Time:
 * Update Comments:
 */
public class CatListAdapter extends BaseQuickAdapter<StoreEntity, BaseViewHolder> {

    private Map<Long, CatListGoodsAdapter> mGoodsListArray;

    private OnCatListAdapterListener mListener;

    public void setOnCatListAdapterListener(OnCatListAdapterListener listener) {
        this.mListener = listener;
    }

    public CatListAdapter() {
        super(R.layout.adapter_cat);

        mGoodsListArray = new HashMap<>();
    }

    @Override
    public void onBindViewHolder(@NonNull BaseViewHolder holder, int position, @NonNull List<Object> payloads) {
        if (payloads != null && payloads.size() > 0) {
            try {
                StoreEntity item = getItem(position - getHeaderLayoutCount());
                FrameLayout cbFl = holder.getView(R.id.cb_fl);
                cbFl.setSelected(item.isSelect());
            } catch (Exception e) {
                super.onBindViewHolder(holder, position, payloads);
            }
        } else {
            super.onBindViewHolder(holder, position, payloads);
        }
    }

    @Override
    protected void convert(BaseViewHolder helper, StoreEntity storeEntity) {
        helper.setText(R.id.tv_shop_name, storeEntity.getName());
        GlideUtil.loadImg(helper.getView(R.id.iv_shop_cover), storeEntity.getLogo(), DensityUtil.dp2px(6));
        RecyclerView goodsRv = helper.getView(R.id.goods_rv);
        goodsRv.setLayoutManager(new LinearLayoutManager(helper.itemView.getContext()));

        CatListGoodsAdapter goodsAdapter = mGoodsListArray.get(storeEntity.getId());
        if (goodsAdapter == null) {
            goodsAdapter = new CatListGoodsAdapter();
            mGoodsListArray.put(storeEntity.getId(), goodsAdapter);
            goodsAdapter.setOnGoodsListAdapterListener(new CatListGoodsAdapter.OnGoodsListAdapterListener() {
                @Override
                public void onItemClick(GoodsEntity goodsEntity, int position) {
                    onGoodsItemClick(goodsEntity, position);
                    if (mListener != null) {
                        mListener.onItemClick(storeEntity, helper.getAdapterPosition());
                    }
                }
            });
            goodsRv.setAdapter(goodsAdapter);
        } else {
            goodsRv.setAdapter(goodsAdapter);
        }
        goodsAdapter.setNewData(storeEntity.getGoodsList());

        helper.getView(R.id.cb_fl).setSelected(storeEntity.isSelect());
        helper.getView(R.id.cb_fl).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemSelect(storeEntity, helper.getAdapterPosition());
                if (mListener != null) {
                    mListener.onItemClick(storeEntity, helper.getAdapterPosition());
                }
            }
        });

        helper.getView(R.id.store_item).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                StoreDetailsActivity.Companion.self(helper.itemView.getContext(), (int) storeEntity.getId(), 0);
            }
        });
    }

    /**
     * Desc:商铺item点击
     * <p>
     * Author: [李豫]
     * Date: 2021-07-15
     *
     * @param storeEntity
     * @param adapterPosition
     */
    private void onItemSelect(StoreEntity storeEntity, int adapterPosition) {
        boolean select = storeEntity.isSelect();
        clearItemSelect();
        storeEntity.setSelect(!select);
        List<GoodsEntity> goodsList = storeEntity.getGoodsList();
        if (goodsList != null) {
            CatListGoodsAdapter goodsAdapter = mGoodsListArray.get(storeEntity.getId());
            for (int i = 0; i < goodsList.size(); i++) {
                GoodsEntity goodsEntity = goodsList.get(i);
                if (select) {
                    if (goodsEntity.isSelect()) {
                        goodsEntity.setSelect(false);
                        goodsAdapter.notifyItemChanged(i, i);
                    }
                } else {
                    if (!goodsEntity.isSelect()) {
                        goodsEntity.setSelect(true);
                        goodsAdapter.notifyItemChanged(i, i);
                    }
                }

            }
        }
        notifyItemChanged(adapterPosition, adapterPosition);
    }

    /**
     * Desc:清空购物车选中
     * <p>
     * Author: [李豫]
     * Date: 2021-07-16
     */
    private void clearItemSelect() {
        List<StoreEntity> data = getData();
        for (int i = 0; i < data.size(); i++) {
            StoreEntity storeItem = data.get(i);
            if (storeItem.isSelect()) {
                storeItem.setSelect(false);
                clearGoodsSelect(storeItem);
                notifyItemChanged(i + getHeaderLayoutCount(), i);
            }
        }
    }

    /**
     * Desc:商品item点击
     * <p>
     * Author: [李豫]
     * Date: 2021-07-15
     *
     * @param goodsEntity
     * @param position
     */
    private void onGoodsItemClick(GoodsEntity goodsEntity, int position) {
        List<StoreEntity> data = getData();
        for (int i = 0; i < data.size(); i++) {
            StoreEntity storeEntity = data.get(i);
            if (storeEntity.getId() == goodsEntity.getShopId()) {
                boolean isAllSelect = checkStoreGoodsIsAllSelect(storeEntity);
                storeEntity.setSelect(isAllSelect);
                notifyItemChanged(i + getHeaderLayoutCount(), i);
            } else {
                if (storeEntity.isSelect()) {
                    storeEntity.setSelect(false);
                    clearGoodsSelect(storeEntity);
                    notifyItemChanged(i + getHeaderLayoutCount(), i);
                }
            }
        }
    }

    /**
     * Desc:清除购物车下商品全选
     * <p>
     * Author: [李豫]
     * Date: 2021-07-16
     *
     * @param storeEntity
     */
    private void clearGoodsSelect(StoreEntity storeEntity) {
        List<GoodsEntity> goodsList = storeEntity.getGoodsList();
        if (goodsList != null) {
            CatListGoodsAdapter goodsAdapter = mGoodsListArray.get(storeEntity.getId());
            for (int i = 0; i < goodsList.size(); i++) {
                GoodsEntity goodsEntity = goodsList.get(i);
                if (goodsEntity.isSelect()) {
                    goodsEntity.setSelect(false);
                    goodsAdapter.notifyItemChanged(i, i);
                }
            }
        }
    }

    /**
     * Desc:商铺商品是否全部选中
     * <p>
     * Author: [李豫]
     * Date: 2021-07-15
     *
     * @param storeEntity
     * @return boolean
     */
    private boolean checkStoreGoodsIsAllSelect(StoreEntity storeEntity) {
        boolean isAllSelect = true;
        List<GoodsEntity> goodsList = storeEntity.getGoodsList();
        if (goodsList != null) {
            for (GoodsEntity goods : goodsList) {
                if (!goods.isSelect()) {
                    return false;
                }
            }
        } else {
            isAllSelect = false;
        }
        return isAllSelect;
    }

    /**
     * Desc:全选购物车下商品
     * <p>
     * Author: [李豫]
     * Date: 2021-07-16
     *
     * @param storeEntity
     * @param isSelect
     */
    private void selectAllGoods(StoreEntity storeEntity, boolean isSelect) {
        List<GoodsEntity> goodsList = storeEntity.getGoodsList();
        if (goodsList != null) {
            CatListGoodsAdapter goodsAdapter = mGoodsListArray.get(storeEntity.getId());
            for (int i = 0; i < goodsList.size(); i++) {
                GoodsEntity goodsEntity = goodsList.get(i);
                if (goodsEntity.isSelect() != isSelect) {
                    goodsEntity.setSelect(isSelect);
                    goodsAdapter.notifyItemChanged(i, i);
                }
            }
        }
    }

    /**
     * Desc:全选
     * <p>
     * Author: [李豫]
     * Date: 2021-07-16
     */
    public void selectAll(boolean isSelect) {
        List<StoreEntity> storeEntities = getData();
        for (int i = 0; i < storeEntities.size(); i++) {
            StoreEntity storeEntity = storeEntities.get(i);
            if (storeEntity.isSelect() != isSelect){
                storeEntity.setSelect(isSelect);
                notifyItemChanged(i+getHeaderLayoutCount(), i);
            }
            selectAllGoods(storeEntity, isSelect);
        }
    }

    /**
     * Desc:获取选中购物车
     * <p>
     * Author: [李豫]
     * Date: 2021-07-16
     *
     * @return store entity
     */
    public StoreEntity getSelectCatShop() {
        List<StoreEntity> storeEntities = getData();
        for (StoreEntity storeEntity : storeEntities) {
            if (storeEntity.isSelect()) {
                return storeEntity;
            }else {
                if (hasSelectGoods(storeEntity)){
                    return storeEntity;
                }
            }
        }
        return null;
    }

    /**
     * Desc:是否有选中的商品
     * <p>
     * Author: [李豫]
     * Date: 2021-07-16
     *
     * @param storeEntity
     * @return boolean
     */
    private boolean hasSelectGoods(StoreEntity storeEntity) {
        List<GoodsEntity> goodsList = storeEntity.getGoodsList();
        for (GoodsEntity goodsEntity: goodsList) {
            if (goodsEntity.isSelect()){
                return true;
            }
        }
        return false;
    }

    public interface OnCatListAdapterListener {
        void onItemClick(StoreEntity storeEntity, int position);
    }
}
